package com.tianqu.tms.www.cardect.controller;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.viewhelper.CarWDPersonTopListViewHelper;

/**
 * 待检测（人工）跳转到车辆基本信息详细页Controller
 * 
 * @author tianshifu
 * 
 */
public class CarWDPersonBaseInfoDetailController extends TmsController {
	private static final String CARSERIAL = "carSerial";// 页面传过来的车辆ID

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			String carSerial = (String) uc.getAttribute(CARSERIAL);
			uc.removeAttribute(CARSERIAL);
			// 根据车辆ID车辆
			TmsDetectInfo entity = TmsDetectInfo.getInstance();

			entity = entity.getWDIpadCarInfoBySerial(carSerial, 1);
			CarWDPersonTopListViewHelper view = new CarWDPersonTopListViewHelper();

			setViewHelpFromEntity(view, entity);

			view.setStatus("待检测(人工) ");

			uc.setAttribute(TmsConstants.VIEWHELP, view);// 用于删除确认页调用
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 将entity值放到viewhelp中
	 * 
	 * @throws TmsDaoException
	 */
	private CarWDPersonTopListViewHelper setViewHelpFromEntity(
			CarWDPersonTopListViewHelper view, TmsDetectInfo entity)
			throws TmsDaoException {
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setRegDate(entity.getRegDate());
		view.setCarCheckInsur(entity.getCarCheckInsur());
		view.setTrafficInsur(entity.getTrafficInsur());

		view.setUpdateTime(entity.getUpdateTime());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));// 毫升转成升+“L”

		Codes code = Codes.getInstance();
		view.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,
				Integer.parseInt(entity.getColor())));
		view.setGearbox(code.getItemValueByItemIdAndItemCode(
				Codes.GEARBOX_TYPE, entity.getGearbox()));
		view.setFuelDict(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE,
				entity.getFuelDict()));
		view.setNature(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,
				entity.getNature()));
		// view.setSource(code.getItemValueByItemIdAndItemCode(DectCodes.CAR_SOURSE,
		// String.valueOf(entity.getSource())));

		return view;
	}

}
